package com.example.myapplication


import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val linearLayout=LinearLayout(this)
        linearLayout.orientation=LinearLayout.VERTICAL

        val img=ImageView(this)
        img.layoutParams=ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        img.setImageResource(R.drawable.flag);//图片添加路径
        linearLayout.addView(img)

        val textView=TextView(this)
        textView.layoutParams=ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        textView.textSize=30F
        textView.text=getString(R.string.hello)
        linearLayout.addView(textView)



        val button=Button(this)
        button.width=ViewGroup.LayoutParams.MATCH_PARENT
        button.height=ViewGroup.LayoutParams.WRAP_CONTENT
        button.text=getString(R.string.click_me)
        linearLayout.addView(button)
        setContentView(linearLayout)
        var  temp=1
        button.setOnClickListener(){
            if(temp%2!=0){
                button.text=getString(R.string.clicked)
                textView.text="世界 你好！"
                temp++
            }else{
                button.text=getString(R.string.click_me)
                textView.text=getString(R.string.hello)
                temp++
            }
        }

//        setContentView(R.layout.activity_main)
//        val button=findViewById<Button>(R.id.button)
//        button.setOnClickListener{
//            val text=findViewById<TextView>(R.id.textView)
//                text.text=getString(R.string.clicked)
//        }
    }
}